﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using vensoft.Entities;
using System.Data;
using System.Data.SqlClient;

namespace vensoft.DataAccess
{
    public class VentaDataAccess : MasterDataAcess , IVentaDataAccess
    {
        IDetalleVentaDataAccess detalleDAL;

        public VentaDataAccess() 
            : base()
        {
            detalleDAL = new DetalleVentaDataAccess(this);
        }

        public void RegistrarVenta(Venta venta)
        {
            SqlCommand command = base.CreateCommand("RegistrarVenta");

            SqlParameter param = command.CreateParameter();
            param.ParameterName = "Codigo_Venta";
            param.Direction = ParameterDirection.InputOutput;
            param.Value = venta.Id;
            
            command.Parameters.Add(param);

            base.AddInParameter(command, "Fecha_Venta", venta.Fecha);
            base.AddInParameter(command, "Cod_Cliente", venta.ClienteId);
            base.AddInParameter(command, "Cod_Personal", venta.PersonalId);
            base.AddInParameter(command, "raz_soc", venta.raz_soc);
            base.AddInParameter(command, "nombre_pers", venta.nombre_Pers);
            base.AddInParameter(command, "Total", venta.Total);

            base.ExecuteCommand(command);

            venta.Id = Int32.Parse(param.Value.ToString());

            detalleDAL.RegistrarDetalle(venta.Detalle, venta.Id);
        }

        public void ActualizarStock(Int32 id_producto, int cantidad)
        {
            SqlCommand command = base.CreateCommand("ActualizarStock");
            base.AddInParameter(command, "Id_Producto", id_producto);
            base.AddInParameter(command, "Cantidad", cantidad);

            base.ExecuteCommand(command);
        }

        public List<Venta> GetAllFromVenta()
        {
            SqlCommand commando = base.CreateSelectCommand("GetAllFromVenta");
            return SelectObjectFactory(commando);
        }

        private List<Venta> SelectObjectFactory(SqlCommand commando)
        {
            List<Venta> lista = new List<Venta>();

            using (SqlDataReader reader = base.ExecuteReader(commando))
            {
                while (reader.Read())
                {
                    Venta venta = new Venta()
                    {
                        Id = reader.GetInt32(0),
                        Fecha = reader.GetDateTime(1),
                        PersonalId = reader.GetInt32(2),
                        ClienteId = reader.GetInt32(3),
                        raz_soc = reader.GetString(4),
                        nombre_Pers = reader.GetString(5),
                        Total = reader.GetDecimal(6)
                    };

                    lista.Add(venta);
                }
            }

            return lista;
        }

        public List<Venta> GetAllFromVentaByFechas(DateTime f_inicio, DateTime f_fin)
        {
            SqlCommand command = base.CreateSelectCommand("GetAllFromVentaByFecha");
            command.Parameters.AddWithValue("f_inicio", f_inicio);
            command.Parameters.AddWithValue("f_fin", f_fin);
            return SelectObjectFactory(command);
        }


        public void CargarDetalle(Venta venta)
        {
            //venta.Cliente =  
            //venta.Detalle = detalleDAL.GetFromDetalleByCodigoVenta(venta.Id);  
        }

        public List<Venta> GetAllFromVentaByNombPers(string nomb_pers)
        {
            SqlCommand command = base.CreateSelectCommand("GetAllFromVentasByNombPers");
            command.Parameters.AddWithValue("nomb_pers", nomb_pers);            
            return SelectObjectFactory(command);
        }
    }
}
